package util.level;

import java.awt.Point;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;

public class TileLayer
{
	private Image image;
	private int tileWidth,tileHeight,tileSpacing;
	private int layerWidth,layerHeight;
	private Point[][] tiles;
	private String name;
	
	/**
	 * Creates a new TileLayer. To be honest, you should never call this, only levelLoader should call this.
	 * @param image - image in which the tile layer is based on.
	 * @param tiles - data containing where in the image corresponds to where in the level.
	 * @param tileWidth - how wide, in pixels, is each tile.
	 * @param tileHeight - how high, in pixels, is each tile.
	 * @param tileSpacing - space between each tile in the tile image.
	 * @param layerWidth - width of the tile array, in tiles.
	 * @param layerHeight - height of the tile array, in tiles.
	 */
	protected TileLayer(Image image,String name,int tileWidth,int tileHeight,int tileSpacing,int layerWidth,int layerHeight)
	{
		this.image=image;
		this.tileWidth=tileWidth;
		this.tileHeight=tileHeight;
		this.tileSpacing=tileSpacing;
		this.layerWidth=layerWidth;
		this.layerHeight=layerHeight;
		this.name=name;
	}
	
	protected void setTiles(Point[][] tiles)
	{
		this.tiles=tiles;
	}
	
	public int getLayerWidthInTiles()
	{
		return layerWidth;
	}
	
	public int getLayerHeightInTiles()
	{
		return layerHeight;
	}
	
	/**
	 * Checks which tile is being drawn at a specific point in tile coordinates. Useful for collisions, like checking
	 * whether or not the returned value is null.
	 * @param x - x position to check-- note that this isn't in room coordinates, but rather tile coordinates.
	 * @param y - y position to check.
	 * @return the Point describing the tile being drawn at this point
	 */
	public Point getTileValue(int x,int y)
	{
		return tiles[x][y];
	}
	
	public String getName()
	{
		return name;
	}
	
	/**
	 * Renders the tile layer.
	 * @param g - current graphics.
	 */
	public void render(Graphics g)
	{
		for(int x=0;x<layerWidth;x++)
			for(int y=0;y<layerHeight;y++)
			{
				Point p=tiles[x][y];
				if(p!=null)
				{
					Image i=image.getSubImage(p.x*(tileWidth+tileSpacing),p.y*(tileHeight+tileSpacing),tileWidth,tileHeight);
					g.drawImage(i,x*tileWidth,y*tileHeight);
				}
			}
	}
}
